<?php

namespace App\Http\Controllers\Api\O1\Auth;

use Biz\Requests\Auth\LoginRequest;
use Biz\Services\Site\SiteMemberService;
use Biz\Services\User\UserMemberService;
use Biz\Transformers\User\UserTransformer;
use Laravel\Lumen\Routing\Controller as BaseController;

/**
 * LoginController.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class LoginController extends BaseController
{
    /**
     * @var mixed
     */
    protected $service;

    /**
     * @var mixed
     */
    protected $siteMemberService;

    /**
     * RepositoryController constructor.
     *
     * @param UserMemberService $service
     * @param SiteMemberService $siteMemberService
     */
    public function __construct(UserMemberService $service, SiteMemberService $siteMemberService)
    {
        $this->service           = $service;
        $this->siteMemberService = $siteMemberService;
    }

    /**
     * Login.
     *
     * @param LoginRequest $request
     *
     * @return string
     */
    public function login(LoginRequest $request)
    {
        $userMember = $this->service->login($request->all());

        $user = $userMember->getUser();
        $user->setMember($userMember);

        // Save Member
        $site_id = $request->get('site_id');
        $this->siteMemberService->save($site_id, $user->id);

        return new UserTransformer($user);
    }
}
